﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BasePlatformCommon;
using BasePlatformNet;

namespace L9SubSet
{
	public partial class Form_ProSet : Form
	{
		public Form_MainSet parent = null;			// 父窗体引用

		public string[] m_ClockList = { };			// 时钟频率数组
		public string[] m_GrayList = { };			// 时钟频率数组
		public int m_SelectedClockIndex = 0;		// 时钟频率选择index
		public int m_SelectedGrayIndex = 0;			// 灰度等级选择index
		public string m_SelectedGrayText;			// 选择的灰度等级文本
		public string m_ClockDuty = "50";			// 占空比
		public string m_GrayStart = "1";			// 缺省起灰值
		public string m_LowGraySmooth = "1";		// 缺省低灰平滑度
		public string m_Gamma = "2.8";				// Gamma值
		public bool m_OpenProtect = false;			// 开机渐亮保护
		public bool m_ColorRevert = false;			// 色彩还原标志
		public bool m_ReciveWidth = false;			// 拓展接收卡截取宽度
		public bool m_NoSignalShowLastFrame = true;	// 无信号时显示最后一帧
		public bool m_PersistCP = false;			// 保持屏体连接和调光调色参数
        public bool m_OutInvert = false;	        // 输出反向

		public int m_BrightR = 255;					// 通道亮度-R
		public int m_BrightG = 255;					// 通道亮度-G
		public int m_BrightB = 255;					// 通道亮度-B
		public int m_BrightW = 255;					// 通道亮度-W

		// 委托定义与声明
		public delegate void SetSubSendButtonEnableDelegate(bool enable);
		public SetSubSendButtonEnableDelegate setSubSendButtonEnableDelegate;
		public delegate void SetApplySendProgressDelegate(int progressValue);
		public SetApplySendProgressDelegate setApplySendProgressDelegate;
		public delegate void SetTestSendProgressDelegate(int progressValue);
		public SetTestSendProgressDelegate setTestSendProgressDelegate;

		private RVCardL9Config m_RvCardL9Config = null;		// 父窗体 sub 配置容器类引用

		private string section = "ProSet";					// 多语言文件配置节名称

		private string tip = "提示";							// MessageBox 题头

		/*****************************************************************************************/

		// 构造函数
		public Form_ProSet(RVCardL9Config m_RvCardL9Config)
		{
			InitializeComponent();

			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
			this.Font = new System.Drawing.Font(this.Font.Name, 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(134)));

			clockDutyTextBox.TextChanged += clockDutyTextBox_TextChanged;
			grayStartTextBox.TextChanged += grayStartTextBox_TextChanged;
			lowGraySmoothTextBox.TextChanged += lowGraySmoothTextBox_TextChanged;
			gammaTextBox.TextChanged += gammaTextBox_TextChanged;
			clockFreqComboBox.SelectedIndexChanged += clockFreqComboBox_SelectedIndexChanged;
			grayLevelComboBox.SelectedIndexChanged += grayLevelComboBox_SelectedIndexChanged;

			setSubSendButtonEnableDelegate = new SetSubSendButtonEnableDelegate(setSubSendButtonEnable);
			setApplySendProgressDelegate = new SetApplySendProgressDelegate(setApplySendProgress);
			setTestSendProgressDelegate = new SetTestSendProgressDelegate(setTestSendProgress);

			this.m_RvCardL9Config = m_RvCardL9Config;
		}

		// 初始化运行环境
		public void initEnvironment()
		{
			// 更新 clocklist
			clockFreqComboBox.Items.Clear();
			clockFreqComboBox.Items.AddRange(m_ClockList);
			clockFreqComboBox.SelectedItem = m_SelectedClockIndex;
			clockFreqComboBox.Text = m_ClockList[m_SelectedClockIndex];

			// 更新灰度等级 List
			grayLevelComboBox.Items.Clear();
			grayLevelComboBox.Items.AddRange(m_GrayList);
			grayLevelComboBox.SelectedIndex = m_SelectedGrayIndex;
			grayLevelComboBox.Text = m_GrayList[m_SelectedGrayIndex];
			m_SelectedGrayText = m_GrayList[m_SelectedGrayIndex];

			// 更新界面上其他参数
			clockDutyTextBox.Text = m_ClockDuty;
			grayStartTextBox.Text = m_GrayStart;
			lowGraySmoothTextBox.Text = m_LowGraySmooth;
			gammaTextBox.Text = m_Gamma;
			openProtectCheckBox.Checked = m_OpenProtect;
			colorRevertCheckBox.Checked = m_ColorRevert;
			spreadWidthCheckBox.Checked = m_ReciveWidth;
			nsLastFrameCheckBox.Checked = m_NoSignalShowLastFrame;
            persistCPCheckBox.Checked = m_PersistCP;
            outInvertCheckBox.Checked = m_OutInvert;

			// 更新通道亮度
			rBrightTextBox.Text = m_BrightR.ToString();
			gBrightTextBox.Text = m_BrightG.ToString();
			bBrightTextBox.Text = m_BrightB.ToString();
			wBrightTextBox.Text = m_BrightW.ToString();

			// 重置按钮及进度条
			applySubButton.Enabled = true;
			testSubButton.Enabled = true;
			resetButton.Enabled = true;
			applySendProgressBar.Value = 0;
			testSendProgressBar.Value = 0;

			// 设置本地化语言
			if (LanguageOper.available == true)
			{
				setLanguageText();
			}
		}

		// 设置本地化语言
		public void setLanguageText()
		{
			if (LanguageOper.available == false)
			{
				return;
			}

			LanguageOper.SetCurrentCulture();

			tip = LanguageOper.ReadValue("Common", "tipStr");

			// 界面控件文字
			this.Text = LanguageOper.ReadValue(section, "proSet.Text");
			perfGroupBox.Text = LanguageOper.ReadValue(section, "perfGroupBox.Text");
			clockFreqLabel.Text = LanguageOper.ReadValue(section, "clockFreqLabel.Text");
			clockDutyLabel.Text = LanguageOper.ReadValue(section, "clockDutyLabel.Text");
			currentLabel.Text = LanguageOper.ReadValue(section, "currentLabel.Text");
			greyTipLabel.Text = LanguageOper.ReadValue(section, "greyTipLabel.Text");
			greyCurveGroupBox.Text = LanguageOper.ReadValue(section, "greyCurveGroupBox.Text");
			gammaLabel.Text = LanguageOper.ReadValue(section, "gammaLabel.Text");
			lowGreyLabel.Text = LanguageOper.ReadValue(section, "lowGreyLabel.Text");
			greyValueLabel.Text = LanguageOper.ReadValue(section, "greyValueLabel.Text");
			colorRevertCheckBox.Text = LanguageOper.ReadValue(section, "colorRevertCheckBox.Text");
			brightGroupBox.Text = LanguageOper.ReadValue(section, "brightGroupBox.Text");
			otherGroupBox.Text = LanguageOper.ReadValue(section, "otherGroupBox.Text");
			spreadWidthCheckBox.Text = LanguageOper.ReadValue(section, "spreadWidthCheckBox.Text");
			persistCPCheckBox.Text = LanguageOper.ReadValue(section, "persistCPCheckBox.Text");
			openProtectCheckBox.Text = LanguageOper.ReadValue(section, "openProtectCheckBox.Text");
			nsLastFrameCheckBox.Text = LanguageOper.ReadValue(section, "nsLastFrameCheckBox.Text");
			applySubButton.Text = LanguageOper.ReadValue(section, "applySubButton.Text");
			testSubButton.Text = LanguageOper.ReadValue(section, "testSubButton.Text");
			resetButton.Text = LanguageOper.ReadValue(section, "resetButton.Text");
			okButton.Text = LanguageOper.ReadValue(section, "okButton.Text");
			cancelButton.Text = LanguageOper.ReadValue(section, "cancelButton.Text");
            outInvertCheckBox.Text = LanguageOper.ReadValue(section, "outInvertCheckBox.Text");
			if (LanguageOper.LocalLanguage != LanguageEnum.EN)
			{
				currentLabel.Location = new Point(191, 61);
				gammaLabel.Location = new Point(20, 29);
				lowGreyLabel.Location = new Point(8, 56);
				greyValueLabel.Location = new Point(32, 83);
				colorRevertCheckBox.Location = new Point(84, 111);
			}
			else
			{
				currentLabel.Location = new Point(180, 61);
				gammaLabel.Location = new Point(44, 29);
				lowGreyLabel.Location = new Point(27, 56);
				greyValueLabel.Location = new Point(15, 83);
				colorRevertCheckBox.Location = new Point(20, 111);
			}
		}

		// 窗体显示后处理
		private void Form_ProSet_Shown(object sender, EventArgs e)
		{
			rBrightTextBox.Text = m_BrightR.ToString();
			gBrightTextBox.Text = m_BrightG.ToString();
			bBrightTextBox.Text = m_BrightB.ToString();
			wBrightTextBox.Text = m_BrightW.ToString();
		}

		// 时钟占空比
		private void clockDutyTextBox_TextChanged(object sender, EventArgs e)
		{
			string msg = "时钟占空比：请输入 [0,100] 范围内的整数";
			if (LanguageOper.available == true)
			{
				msg = LanguageOper.ReadValue(section, "msgStr.0");
			}

			try
			{
				int tmpColokDuty = Int32.Parse(clockDutyTextBox.Text);
				if (tmpColokDuty < 0 || tmpColokDuty > 100)
				{
					MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.None);
					clockDutyTextBox.Text = m_ClockDuty;
					clockDutyTextBox.SelectAll();
					return;
				}
			}
			catch
			{
				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.None);
				clockDutyTextBox.Text = m_ClockDuty;
				clockDutyTextBox.SelectAll();
				return;
			}

			m_ClockDuty = clockDutyTextBox.Text;
		}

		// 起灰值
		private void grayStartTextBox_TextChanged(object sender, EventArgs e)
		{
			string msg = "起灰值：请输入非负整数";
			if (LanguageOper.available == true)
			{
				msg = LanguageOper.ReadValue(section, "msgStr.1");
			}

			try
			{
				int tmpGrayStart = Int32.Parse(grayStartTextBox.Text);
				if (tmpGrayStart < 0)
				{
					
					MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.None);
					grayStartTextBox.Text = m_GrayStart;
					grayStartTextBox.SelectAll();
					return;
				}
			}
			catch
			{
				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.None);
				grayStartTextBox.Text = m_GrayStart;
				grayStartTextBox.SelectAll();
				return;
			}

			m_GrayStart = grayStartTextBox.Text;
		}

		// 低灰平滑度
		private void lowGraySmoothTextBox_TextChanged(object sender, EventArgs e)
		{
			string msg = "低灰平滑度：请输入非负整数";
			if (LanguageOper.available == true)
			{
				msg = LanguageOper.ReadValue(section, "msgStr.2");
			}

			try
			{
				int tmpLowGraySmooth = Int32.Parse(lowGraySmoothTextBox.Text);
				if (tmpLowGraySmooth < 0)
				{
					MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.None);
					lowGraySmoothTextBox.Text = m_LowGraySmooth;
					lowGraySmoothTextBox.SelectAll();
					return;
				}
			}
			catch
			{
				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.None);
				lowGraySmoothTextBox.Text = m_LowGraySmooth;
				lowGraySmoothTextBox.SelectAll();
				return;
			}

			m_LowGraySmooth = lowGraySmoothTextBox.Text;
		}

		// Gamma值
		private void gammaTextBox_TextChanged(object sender, EventArgs e)
		{
			string msg = "Gamma值：请输入非负值（整数/小数）";
			if (LanguageOper.available == true)
			{
				msg = LanguageOper.ReadValue(section, "msgStr.3");
			}

			try
			{
				double tmpGamma = Double.Parse(gammaTextBox.Text);
				if (tmpGamma < 0)
				{
					MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.None);
					gammaTextBox.Text = m_Gamma;
					gammaTextBox.SelectAll();
					return;
				}
			}
			catch
			{
				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.None);
				gammaTextBox.Text = m_Gamma;
				gammaTextBox.SelectAll();
				return;
			}

			m_Gamma = gammaTextBox.Text;
		}

		// 色彩还原
		private void colorRevertCheckBox_CheckedChanged(object sender, EventArgs e)
		{
			if (colorRevertCheckBox.Checked == true)
			{
				int grayLevel = -1;
				int.TryParse(grayLevelComboBox.Text, out grayLevel);
				if (grayLevel > 1024)
				{
					m_ColorRevert = colorRevertCheckBox.Checked;
				}
				else
				{
					string msg = "灰度等级小于等于 1024 时，禁用色彩还原";
					if (LanguageOper.available == true)
					{
						msg = LanguageOper.ReadValue(section, "msgStr.4");
					}

					MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.None);
					
					colorRevertCheckBox.Checked = false;
				}
			}
		}

		// 开屏渐亮保护
		private void openProtectCheckBox_CheckedChanged(object sender, EventArgs e)
		{
			m_OpenProtect = openProtectCheckBox.Checked;
		}

		// 拓展接收卡截取宽度
		private void spreadWidthCheckBox_CheckedChanged(object sender, EventArgs e)
		{
			m_ReciveWidth = spreadWidthCheckBox.Checked;
		}

		// 无信号时显示最后一帧
		private void nsLastFrameCheckBox_CheckedChanged(object sender, EventArgs e)
		{
			m_NoSignalShowLastFrame = nsLastFrameCheckBox.Checked;
		}

        // 输出反向
        private void outInvertCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            m_OutInvert = outInvertCheckBox.Checked;
        }

        // 保持屏体连接和调光调色参数
		private void persistCPCheckBox_CheckedChanged(object sender, EventArgs e)
		{
			m_PersistCP = persistCPCheckBox.Checked;
		}

		// 时钟频率
		private void clockFreqComboBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			m_SelectedClockIndex = clockFreqComboBox.SelectedIndex;
		}

		// 灰度等级
		private void grayLevelComboBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			m_SelectedGrayIndex = grayLevelComboBox.SelectedIndex;
		}

		// 确定
		private void okButton_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.OK;
		}

		// 取消
		private void cancelButton_Click(object sender, EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
		}

		// 通道亮度滑块 - R
		private void rBrightTrackBar_Scroll(object sender, EventArgs e)
		{
			rBrightTextBox.Text = rBrightTrackBar.Value.ToString();
		}

		// 通道亮度滑块 - G
		private void gBrightTrackBar_Scroll(object sender, EventArgs e)
		{
			gBrightTextBox.Text = gBrightTrackBar.Value.ToString();
		}

		// 通道亮度滑块 - B
		private void bBrightTrackBar_Scroll(object sender, EventArgs e)
		{
			bBrightTextBox.Text = bBrightTrackBar.Value.ToString();
		}

		// 通道亮度滑块 - W
		private void wBrightTrackBar_Scroll(object sender, EventArgs e)
		{
			wBrightTextBox.Text = wBrightTrackBar.Value.ToString();
		}

		// 通道亮度填写 - R
		private void rBrightTextBox_TextChanged(object sender, EventArgs e)
		{
			int orignValue = m_BrightR;
			int value = -1;
			bool res = int.TryParse(rBrightTextBox.Text, out value);
			if (res == false || value < 0 || value > 255)
			{
				string msg = "通道亮度设置：请输入 [0,255] 范围内的整数";
				if (LanguageOper.available == true)
				{
					msg = LanguageOper.ReadValue(section, "msgStr.5");
				}

				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.None);
				
				rBrightTextBox.Text = orignValue.ToString();
				rBrightTextBox.SelectAll();
			}
			else
			{
				m_BrightR = value;
				rBrightTrackBar.Value = value;
			}
		}

		// 通道亮度填写 - G
		private void gBrightTextBox_TextChanged(object sender, EventArgs e)
		{
			int orignValue = m_BrightG;
			int value = -1;
			bool res = int.TryParse(gBrightTextBox.Text, out value);
			if (res == false || value < 0 || value > 255)
			{
				string msg = "通道亮度设置：请输入 [0,255] 范围内的整数";
				if (LanguageOper.available == true)
				{
					msg = LanguageOper.ReadValue(section, "msgStr.5");
				}

				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.None);

				gBrightTextBox.Text = orignValue.ToString();
				gBrightTextBox.SelectAll();
			}
			else
			{
				m_BrightG = value;
				gBrightTrackBar.Value = value;
			}
		}

		// 通道亮度填写 - B
		private void bBrightTextBox_TextChanged(object sender, EventArgs e)
		{
			int orignValue = m_BrightB;
			int value = -1;
			bool res = int.TryParse(bBrightTextBox.Text, out value);
			if (res == false || value < 0 || value > 255)
			{
				string msg = "通道亮度设置：请输入 [0,255] 范围内的整数";
				if (LanguageOper.available == true)
				{
					msg = LanguageOper.ReadValue(section, "msgStr.5");
				}

				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.None);

				bBrightTextBox.Text = orignValue.ToString();
				bBrightTextBox.SelectAll();
			}
			else
			{
				m_BrightB = value;
				bBrightTrackBar.Value = value;
			}
		}

		// 通道亮度填写 - W
		private void wBrightTextBox_TextChanged(object sender, EventArgs e)
		{
			int orignValue = m_BrightW;
			int value = -1;
			bool res = int.TryParse(wBrightTextBox.Text, out value);
			if (res == false || value < 0 || value > 255)
			{
				string msg = "通道亮度设置：请输入 [0,255] 范围内的整数";
				if (LanguageOper.available == true)
				{
					msg = LanguageOper.ReadValue(section, "msgStr.5");
				}

				MessageBoxEx.Show(this, msg, tip, MessageBoxButtons.OK, MessageBoxIcon.None);

				wBrightTextBox.Text = orignValue.ToString();
				wBrightTextBox.SelectAll();
			}
			else
			{
				m_BrightW = value;
				wBrightTrackBar.Value = value;
			}
		}

		// 应用
		private void applySubButton_Click(object sender, EventArgs e)
		{
			updateRvCardL9Config();
			parent.applySubButtonPerformClick();
		}

		// 测试
		private void testSubButton_Click(object sender, EventArgs e)
		{
			updateRvCardL9Config();
			parent.testSubButtonPerformClick();
		}

		// 复位分控
		private void resetButton_Click(object sender, EventArgs e)
		{
			parent.resetButtonPerformClick();
		}

		// 设置 应用/测试/复位分控 按钮状态
		private void setSubSendButtonEnable(bool enable)
		{
			applySubButton.Enabled = enable;
			testSubButton.Enabled = enable;
			resetButton.Enabled = enable;
		}

		// 更新 sub 文件下发进度 - 应用
		private void setApplySendProgress(int progressValue)
		{
			if (progressValue >= 0 && progressValue <= 100)
			{
				applySendProgressBar.Value = progressValue;
				if (progressValue == 0)
				{
					applySubButton.Enabled = true;
					testSubButton.Enabled = true;
					resetButton.Enabled = true;
				}
				else
				{
					applySubButton.Enabled = false;
					testSubButton.Enabled = false;
					resetButton.Enabled = false;
				}
			}
		}

		// 更新 sub 文件下发进度 - 测试
		private void setTestSendProgress(int progressValue)
		{
			if (progressValue >= 0 && progressValue <= 100)
			{
				testSendProgressBar.Value = progressValue;
				if (progressValue == 0)
				{
					applySubButton.Enabled = true;
					testSubButton.Enabled = true;
					resetButton.Enabled = true;
				}
				else
				{
					applySubButton.Enabled = false;
					testSubButton.Enabled = false;
					resetButton.Enabled = false;
				}
			}
		}

		// 根据当前界面值更新 m_RvCardL9Config 中的信息
		private void updateRvCardL9Config()
		{
			// 更新时钟频率
			m_RvCardL9Config.clockFreqIndex = m_SelectedClockIndex;

			// 更新占空比
			m_RvCardL9Config.clockDuty = Convert.ToDouble(m_ClockDuty) / 100.0;

			// 更新灰度等级
			m_RvCardL9Config.SetGrayIndex(m_SelectedGrayIndex);

			// 更新 Gamma 值
			double m_GammaValue = Convert.ToDouble(m_Gamma);
			if (m_GammaValue == 0)
			{
				m_GammaValue = 2.8;
			}
			else if (m_GammaValue > 10)
			{
				m_GammaValue = 10;
			}
			m_RvCardL9Config.SetGamma(m_GammaValue);

			// 更新起灰值
			int m_GrayStartValue = Convert.ToInt32(m_GrayStart);
			if (m_GrayStartValue > 255)
			{
				m_GrayStartValue = 255;
			}
			m_RvCardL9Config.SetGrayStart(m_GrayStartValue);

			// 更新低灰平滑度
			m_RvCardL9Config.SetLowGraySmooth(Convert.ToInt32(m_LowGraySmooth));

			// 更新开机渐亮保护状态
			int state = (m_OpenProtect == false ? 0 : 1);
			m_RvCardL9Config.powerOnProtect = state;

			// 更新色彩还原
			if (m_ColorRevert == true)
			{
				m_RvCardL9Config.colorReverse = 1;
			}
			else
			{
				m_RvCardL9Config.colorReverse = 0;
			}

			// 更新拓展接收卡截取宽度
			if (m_ReciveWidth == true)
			{
				m_RvCardL9Config.receiveCardWidth = 1;
			}
			else
			{
				m_RvCardL9Config.receiveCardWidth = 0;
			}

			// 更新无信号时显示最后一帧
			if (m_NoSignalShowLastFrame == true)
			{
				m_RvCardL9Config.noSignalMode = 1;
			}
			else
			{
				m_RvCardL9Config.noSignalMode = 0;
			}

			// 更新保持屏体连接和调光调色参数
			if (m_PersistCP == true)
			{
				m_RvCardL9Config.persistCP = 1;
			}
			else
			{
				m_RvCardL9Config.persistCP = 0;
			}

            // 更新输出反向
            if (m_OutInvert == true)
            {
                m_RvCardL9Config.outInvert = 1;
            }
            else
            {
                m_RvCardL9Config.outInvert = 0;
            }

            // 判断当前的灰度等级是否<=1024，若<=1024，则禁用颜色还原功能
			int grayLevel = Convert.ToInt32(m_GrayList[m_SelectedGrayIndex]);
			if (grayLevel <= 1024)
			{
				m_RvCardL9Config.colorReverse = 0;
				m_ColorRevert = false;
			}

			// 更新通道亮度 - R/G/B/W
			m_RvCardL9Config.brightR = m_BrightR;
			m_RvCardL9Config.brightG = m_BrightG;
			m_RvCardL9Config.brightB = m_BrightB;
			m_RvCardL9Config.brightW = m_BrightW;
		}
	}
}
